﻿using BUS;
using DAO;
using OnlineDigitalCameraWebsite.Class;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineDigitalCameraWebsite
{
    public partial class ThongTinTaiKhoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            bool b = (bool)Session["DaDangNhap"];
            if (b == true)
            {
                if (!IsPostBack)
                {
                    LoadThongTin();
                }
            }
            else
            {
                Response.Redirect("/DangNhap.aspx");
            }
        }

        private void LoadThongTin()
        {
            UsersBUS uBUS = new UsersBUS();
            User u = new User();
            int idu = int.Parse(Session["UserID"].ToString());
            u = uBUS.GetByUserId(idu);

            txt_Username.Text = u.userAccount;
            txt_HoTen.Text = u.userName;
            txtEmail.Text = u.userEmail;
            txt_DienThoai.Text = u.userPhone;
            txt_DiaChi.Text = u.userAddress;

        }

      

        protected void btn_DangKy_Click(object sender, EventArgs e)
        {
            string username = txt_Username.Text;
            UsersBUS uBUS = new UsersBUS();
            User user = new User();
            int idu = int.Parse(Session["UserID"].ToString());
            user.userId = idu;
            user.userAccount = username;
            user.userPass = XuLy.MaHoaMatKhau(txt_Password.Text);
            user.userName = txt_HoTen.Text;
            user.userEmail = txtEmail.Text;
            user.userPhone = txt_DienThoai.Text;
            user.userAddress = txt_DiaChi.Text;
            user.userStatus = true;

            if (uBUS.Edit(user))
            {
                ShowMessage("Cập nhật thông tin thành công.");
                Session["HoTen"] = user.userName ;
                Session["Username"] = user.CommentAccessories;
                Session["UserID"] = user.userId;
                LoadThongTin();
            }
            else
            {
                ShowMessage("Cập nhật thông tin thất bại");
            }

           
        }
        public void ShowMessage(string Content)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('" + Content + "');</script>");
        }
    }
}